package cn.dgut.vinko.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;

import cn.dgut.vinko.domain.Employee;
import cn.dgut.vinko.service.EmpService;

@Controller
@RequestMapping("/jxc")
public class EmpController {
	@Autowired
	private EmpService service;

	@RequestMapping("loginPage")
	public ModelAndView redirectLogin() {
		return new ModelAndView("login");
	}

	@RequestMapping("registerPage")
	public ModelAndView redirectRegister() {
		return new ModelAndView("register");
	}

	@RequestMapping("login")
	public ModelAndView login(HttpSession httpSession, Employee employee) {

		if (service.login(employee)) {
			// 设置session
			httpSession.setAttribute("empName", employee.getEmpName());
			return new ModelAndView("admin").addObject("employee", employee);
		} else {
			return new ModelAndView("login").addObject("message", "用户名或密码错误");
		}
	}

	@RequestMapping("register")
	public ModelAndView register(Employee employee) {
		if (service.register(employee)) {
			return new ModelAndView("admin").addObject(employee);
		} else {
			return new ModelAndView("register").addObject("message", "the name is exsit!");
		}
	}

}
